package com.liu.communitysystemend.module.complaints.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liu.communitysystemend.exception.CommonException;
import com.liu.communitysystemend.exception.SavaException;
import com.liu.communitysystemend.module.complaints.domain.Complaints;
import com.liu.communitysystemend.module.complaints.domain.ComplaintsDto;
import com.liu.communitysystemend.module.complaints.mapper.ComplaintsMapper;
import com.liu.communitysystemend.module.complaints.service.ComplaintsService;
import com.liu.communitysystemend.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 */
@Service
public class ComplaintsServiceImpl implements ComplaintsService {

    @Autowired
    private ComplaintsMapper complaintsMapper;

    @Autowired
    private AuthUtils authUtils;

    @Override
    public Complaints getComplaintsById(String id) {
        if (id == null) {
            throw new CommonException(501, "参数为空");
        }
        LambdaQueryWrapper<Complaints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Complaints::getId, id);
        Complaints complaints = complaintsMapper.selectOne(wrapper);
        return complaints;
    }

    @Override
    public Complaints update(Complaints complaints) {
        LambdaQueryWrapper<Complaints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Complaints::getId, complaints.getId());
        int update = complaintsMapper.update(complaints, wrapper);
        if (update > 0) {
            return complaints;
        }
        throw new CommonException(502, "更新失败");
    }

    @Override
    public Complaints add(Complaints complaints) {
        String uid = authUtils.getUid();
        complaints.setUserId(uid);
        complaints.setComplaintsTime(new Date());
        complaints.setStatus(0);
        if (complaints == null) {
            throw new SavaException("插入用户失败,菜单的类型实体为空");
        }
        int insert = complaintsMapper.insert(complaints);
        if (insert > 0) {
            return complaints;
        }
        throw new SavaException("插入用户失败");
    }

    @Override
    public Integer deleteById(String id) {
        Complaints complaints = selectOneById(id);
        if (complaints == null) {
            throw new CommonException(4004, "找不到id为:" + id + "的数据");
        }
        int i = complaintsMapper.deleteById(id);
        return i;
    }

    @Override
    public Page<Complaints> getPage(Complaints complaints) {
        LambdaQueryWrapper<Complaints> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Complaints::getUserId, complaints.getUserId());
        wrapper.orderByDesc(Complaints::getComplaintsTime);
        wrapper.eq(Complaints::getIsDeleted, 0);
        Page<Complaints> page = new Page<>(complaints.getPage().getPage(), complaints.getPage().getSize());
        Page<Complaints> poiPage = (Page<Complaints>) complaintsMapper.selectPage(page, wrapper);
        return poiPage;
    }


    public Complaints selectOneById(String id) {
        Complaints complaints = complaintsMapper.selectById(id);
        return complaints;
    }

    @Override
    public Integer test() {

        for (int i = 0; i < 20; i++) {
            Complaints complaints = new Complaints();
            complaints.setId("100" + i);
            complaints.setComplaintsTime(new Date());
            complaints.setReason("隔壁太吵了" + i);
            complaints.setStatus(0);
            complaints.setUserId("100" + i);
            complaintsMapper.insert(complaints);
        }
        return 1;
    }

    @Override
    public Page<ComplaintsDto> getAllPage(Complaints complaints) {
        Page<ComplaintsDto> page = new Page<>(complaints.getPage().getPage(), complaints.getPage().getSize());
        return complaintsMapper.getAllPage(page, complaints);

    }

}
